// H指数2

package Leetcode;

class Solution_275_1 {
    public int hIndex(int[] citations) {
        int h = 0; // h从后往前逐渐递增
        int i = citations.length - 1;
        // 比较 citations[i] > h 如果成立，则会存在 h+1 个大于 h 的书籍
        while (i >= 0 && citations[i] > h) {
            h++;
            i--;
        }
        return h;
    }
}

class Solution_275_2 {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int left = 0, right = n - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (citations[mid] >= n - mid) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return n - left;
    }
}